package com.hpin.mlxteacher.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.BabyPagerAdapter;
import com.hpin.mlxteacher.base.BaseFragment;
import com.hpin.mlxteacher.child.ChildListActivity;
import com.hpin.mlxteacher.child.WorkActivity;
import com.hpin.mlxteacher.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 孩子fragment
 * Created by Administrator on 2015/7/15.
 */
public class ChildFragment extends BaseFragment implements View.OnClickListener {

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    /**
     * 我的定位坐标的点
     */
    private double myLatitude;
    private double myLongitude;
    private LocationMode mCurrentMode;
    private MyLocationListenner myListener = new MyLocationListenner();
    // 定位相关
    private LocationClient mLocClient;
    //是否是第一次定位
    private boolean isFirstLoc = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View childView = View.inflate(getActivity(), R.layout.fragment_child, null);

        initWidget(childView);

        //设为第一次进入定位
        isFirstLoc = true;

        return childView;
    }

    private void initWidget(View childView) {

        TextView tv_left = (TextView) childView.findViewById(R.id.tv_left);
        TextView tv_title = (TextView) childView.findViewById(R.id.tv_title);
        TextView tv_right = (TextView) childView.findViewById(R.id.tv_right);

        tv_left.setVisibility(View.VISIBLE);
        tv_right.setVisibility(View.VISIBLE);

        tv_left.setText(getResources().getString(R.string.child_work));
        tv_title.setText(getResources().getString(R.string.rl_child));
        tv_right.setText(getResources().getString(R.string.child_list));

//        RecyclerView recyclerView = (RecyclerView) childView.findViewById(R.id.rv_child);
//
//        // 创建一个线性布局管理器
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
//        //设置为水平方向
//        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        // 设置布局管理器
//        recyclerView.setLayoutManager(layoutManager);
//        //设置adapter
//        ChildChooseAdapter adapter = new ChildChooseAdapter(getActivity());
//        recyclerView.setAdapter(adapter);

        ViewPager vp_child = (ViewPager) childView.findViewById(R.id.vp_child);

        List<View> viewList = new ArrayList<View>();
        for (int i = 0; i < 30; i++) {

            View v = View.inflate(getActivity(), R.layout.item_choose_child, null);
            viewList.add(v);
        }

        BabyPagerAdapter pagerAdapter = new BabyPagerAdapter(viewList);
        vp_child.setAdapter(pagerAdapter);

        vp_child.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                LogUtil.e("TAG", "PAGE SELECT===::" + position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });




        // 地图初始化
        mMapView = (MapView) childView.findViewById(R.id.bmapView);

        // 获取地图控件引用
        mBaiduMap = mMapView.getMap();


        getMyLocation();


        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

    }


    /**
     * 获取到我的当前位置
     */
    private void getMyLocation() {
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_center_point_my);
        mCurrentMode = LocationMode.NORMAL;
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
//       //定位的点击事件
//        mBaiduMap.setOnMyLocationClickListener(new OnMyLocationClickListener() {
//
//            @Override
//            public boolean onMyLocationClick() {
//
//                LatLng latLng = new LatLng(myLatitude, myLongitude);
//                showMyLocationInfo(latLng);
//                return false;
//            }
//        });

        mLocClient = new LocationClient(getActivity());
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(10000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                //工作

                Intent intent = new Intent(getActivity(), WorkActivity.class);
                getActivity().startActivity(intent);

                break;
            case R.id.tv_right:
                //列表

                Intent mIntent = new Intent(getActivity(), ChildListActivity.class);
                startActivity(mIntent);
                break;
        }
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null)
                return;
            MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();

            myLatitude = location.getLatitude();
            myLongitude = location.getLongitude();

            mBaiduMap.setMyLocationData(locData);


            if (isFirstLoc) {
                isFirstLoc = false;
//                //如果未绑定设备则定位到我的位置,否则定位到手表的位置
//                if (TextUtils.isEmpty(currentWatchId)) {

                try {
                    LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                    MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                    if (mBaiduMap != null)
                        mBaiduMap.animateMapStatus(u);
                } catch (Exception e) {
                    e.printStackTrace();

                }

//                }
            }

        }


    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
        // 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

}
